package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DeletOpertion implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("请输入要删除书的书名：");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        System.out.println("请输入要删除书的作者：");
        String author = scanner.nextLine();

        System.out.println("请输入要删除书的类型：");
        String type = scanner.nextLine();

        System.out.println("请输入要删除书的价格：");
        int price = scanner.nextInt();

        Book book = new Book(name,author,type,price);

        //看这本书是否已经存在,记住位置
        int now = -1;
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            if(bookList.getBooks()[i].equals(book)){
                now = i;
                break;
            }
        }
        //覆盖
        if(now != -1){
            for (int i = now; i < bookList.getUsedSize() - 1; i++) {
                bookList.getBooks()[i] = bookList.getBooks()[i+1];
            }
            bookList.setUsedSize(bookList.getUsedSize() - 1);
            System.out.println("删除成功");
        }else{
            System.out.println("没有该书");
        }
    }
}
